package utility;

import java.io.Serializable;

/**
 * Represents a pair of two objects
 * @author Danon
 */
public class Pair<A, B> implements Serializable {
    public final long serialVersionID = 1L;
    
    public Pair(A a, B b) {
        this.a = a;
        this.b = b;
    }
    
    public final A getA() {
        return a;
    }
    
    public final B getB() {
        return b;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + (this.a != null ? this.a.hashCode() : 0);
        hash = 59 * hash + (this.b != null ? this.b.hashCode() : 0);
        return hash;
    }
    
    /**
     * Compares this pair with specified Object o.
     * @param o Object to compare with
     * @return true - if pair contains the same elements (this.a == o.a AND this.b == o.b), otherwise - false
     */
    public final boolean equals(Object o) {
        if(o==null) return false;
        if(o instanceof Pair) {
            Pair<A, B> p = (Pair<A, B>)o;
            //TODO: Check p.getA() and p.getB() for null value
            if(p.getA()==null || p.getB()==null) return p.hashCode()==hashCode();
            return(this.getA().equals(p.a) && this.getB().equals(p.b));
        } else return false;
    }
    
    @Override
    public String toString() {
        return "(" + a +"; " + b + ")";
    }
    
    private A a;
    private B b;
}

